/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Monetario;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


/**
 *
 * @author Edson Salazar
 */
public class conexion2 {

    private static String servidor = "169.254.50.243";
    private static String bd = "BANCO";
    private static String usuario = "bases";
    private static String pass = "bases2";

    public static boolean conectar(){
        //String url = "jdbc:mySubprotocol:myDataSource";
        String url = "jdbc:jtds:sqlserver://"+servidor+"/"+bd;
        Connection con;
        String createString = "SELECT * FROM AGENCIA";
        Statement stmt;

        try { //Cargamos el driver
            //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
        } catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
            return false;
        }

        try { //Establecemos la conexión
            //con = DriverManager.getConnection(url, "myLogin", "myPassword");
            con = DriverManager.getConnection(url,usuario,pass);
            //Creamos el ststement
            stmt = con.createStatement();
            //Ejecuta una actualización: crear tablas , insertar , delete ,...
            stmt.executeQuery(createString);
            System.out.println("Consulta realizada con éxito");
            stmt.close();
            con.close();
            // stmt.executeQuery() : consultas (select)
        } catch(java.sql.SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
            return false;
        } catch (Exception ex2){
            System.err.println(ex2.getMessage());
            return false;
        }
        return true;
    }
    public static boolean checkLogin(String user,String pass,int t){
        //String url = "jdbc:mySubprotocol:myDataSource";
        String url = "jdbc:jtds:sqlserver://"+servidor+"/"+bd;
        Connection con;
        boolean valido=false;
        ResultSet rs=null;
        String createString = "USE BANCO;  Select * FROM USUARIO WHERE alias='"+user+"' and password='"+pass+"';";
        Statement stmt;

        if(user.contains("'") ||pass.contains("'")||pass.contains(";")||user.contains(";")){

        }
        else{
        try { //Cargamos el driver
            //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
        } catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());

        }

        try { //Establecemos la conexión
            //con = DriverManager.getConnection(url, "myLogin", "myPassword");
            con = DriverManager.getConnection(url,usuario,pass);
            //Creamos el ststement
            stmt = con.createStatement();
            //Ejecuta una actualización: crear tablas , insertar , delete ,...
            rs = stmt.executeQuery(createString);
            if(rs.next()){
                if(rs.getString(7).matches(String.valueOf(t)))
                valido=true;
            }
            stmt.close();
            con.close();
            // stmt.executeQuery() : consultas (select)
        } catch(java.sql.SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());

        } catch (Exception ex2){
            System.err.println(ex2.getMessage());

        }
    }
        return valido;

    }
        public static boolean insertar_cuenta(String parametros,int t){
        //String url = "jdbc:mySubprotocol:myDataSource";
        String url = "jdbc:jtds:sqlserver://"+servidor+"/"+bd;
        Connection con;
        boolean valido=false;
        ResultSet rs=null;
        String createString = "USE BANCO; exec INSERTAR_CUENTA "+parametros;
        Statement stmt;

        if(parametros.contains(";")|| parametros.contains("(") ){

        }
        else{
        try { //Cargamos el driver
            //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
        } catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());

        }

        try { //Establecemos la conexión
            //con = DriverManager.getConnection(url, "myLogin", "myPassword");
            con = DriverManager.getConnection(url,usuario,pass);
            //Creamos el ststement
            stmt = con.createStatement();
            //Ejecuta una actualización: crear tablas , insertar , delete ,...
            rs = stmt.executeQuery(createString);
            if(rs.next()){
                if(rs.getString(7).matches(String.valueOf(t)))
                valido=true;
            }
            stmt.close();
            con.close();
            // stmt.executeQuery() : consultas (select)
        } catch(java.sql.SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());

        } catch (Exception ex2){
            System.err.println(ex2.getMessage());

        }
    }
        return valido;

    }
}
